package com.ck.company.controller.client;

import com.ck.annotation.PermissionRequired;
import com.ck.company.service.CpPictureGroupService;
import com.ck.constant.RequestConstant;
import com.ck.constant.ResponseConstant;
import com.ck.exception.ServiceException;
import com.ck.model.common.User;
import com.ck.model.common.sys.JsonResponse;
import com.ck.model.company.dto.ProgramPictureGroupTreeDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片组
 *
 * @author linqiang
 * @date 2018-09-19 18:19
 */
@PermissionRequired
@Controller
@RequestMapping("/client/picture/group/")
public class CpPictureGroupClientController {
    private static Logger logger = LoggerFactory.getLogger(CpPictureGroupClientController.class);
    @Autowired
    private CpPictureGroupService cpPictureGroupService;


    /**
     * 获取图片分组树(节目上传素材用)
     */
    @ResponseBody
    @RequestMapping(value = "/tree", method = RequestMethod.POST)
    public Object tree(HttpServletRequest request, Long pictureGroupId) {
        User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
        try {
            List<ProgramPictureGroupTreeDTO> pictureGroupTreeDTOS = cpPictureGroupService.listProgramPictureGroupTree(
                    loginUser.getCompanyCode(), pictureGroupId);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS,
                    pictureGroupTreeDTOS != null ? pictureGroupTreeDTOS : new ArrayList<>(),
                    "查询成功");
        } catch (ServiceException e) {
            e.printStackTrace();
            logger.error("CpPictureGroupClientController tree 获取图片分组树失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询失败");
    }

    /**
     * 根据分组ID获取分组
     */
    @ResponseBody
    @RequestMapping(value = "/get/{pictureGroupId}", method = RequestMethod.POST)
    public Object getById(HttpServletRequest request, @PathVariable("pictureGroupId") Long pictureGroupId) {
        try {
            User loginUser = (User) request.getSession().getAttribute(RequestConstant.SESSION_KEY_USER);
            if (null == pictureGroupId) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "查询失败");
            }
            ProgramPictureGroupTreeDTO pictureGroupTreeDTO = cpPictureGroupService.getPictureGroupTreeById(
                    loginUser.getCompanyCode(), pictureGroupId);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, pictureGroupTreeDTO, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpPictureGroupClientController getById 获取分组失败：" + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询失败");
    }
}
